//
//  TSWithDrawHeaderView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSWithDrawItem: UIView {
    
    var priceLabel : UILabel!
    var titleLabel : UILabel!
    
    convenience init(_ title:String) {
        self.init()
        self.configViews(title: title)
    }
    
    func setupView(price:String) {
        
        priceLabel.text = price
    }
    
    func configViews(title:String) {
        
        priceLabel = UILabel.init()
        priceLabel.text = "￥0.00"
        priceLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#454545")
        priceLabel.font = UIFont.systemFont(ofSize: STATUSBAR_HEIGHT > 20 ? 18 : 14, weight: .medium)
        priceLabel.adjustsFontSizeToFitWidth = true
        priceLabel.minimumScaleFactor = 0.6
        priceLabel.textAlignment = .center
        self.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(17)
            make.left.equalTo(5)
            make.right.equalTo(-5)
        }
        
        titleLabel = UILabel.init()
        titleLabel.text = title
        titleLabel.textColor = BASE_GRAY_COLOR
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.minimumScaleFactor = 0.6
        titleLabel.textAlignment = .center
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(priceLabel.snp.bottom).offset(7)
        }
    }
}

class TSWithDrawHeaderView: UIView {

    var avatar : UIImageView!
    var nameLabel : UILabel!
    var identifyLabel : UILabel!
    var profitItem : TSWithDrawItem!
    var gainItem : TSWithDrawItem!
    var storeItem : TSWithDrawItem!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func refreshHeaderView() {
        
        profitItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        gainItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        storeItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
    }
    
    func configViews() {
        
        let scale : CGFloat = 375 / 140.0
        let containerView = UIImageView.init(image: UIImage(named: "general_header_bg"))
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(SCREEN_WIDTH / scale)
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = WHITE_COLOR
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        containerView.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalTo(26*T_ScreenScale())
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
        nameLabel.textColor = WHITE_COLOR
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        containerView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar.snp.right).offset(13)
            make.top.equalTo(avatar.snp.top).offset(7*T_ScreenScale())
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = WHITE_COLOR
        identifyLabel.font = UIFont.systemFont(ofSize: 12)
        containerView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(10*T_ScreenScale())
        }
        
//        let containerView = UIView.init()
//        containerView.backgroundColor = BASE_PURPLE_COLOR
//        containerView.layer.cornerRadius = 10
//        containerView.layer.masksToBounds = true
//        self.addSubview(containerView)
//        containerView.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(17)
//            make.width.equalTo(SCREEN_WIDTH - 2*12)
//            make.height.equalTo(191)
//        }

//        avatar = UIImageView.init()
//        avatar.image = UIImage(named: "general_avatar_default")
//        avatar.backgroundColor = WHITE_COLOR
//        avatar.layer.cornerRadius = 30
//        avatar.layer.masksToBounds = true
//        containerView.addSubview(avatar)
//        avatar.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(15)
//            make.size.equalTo(CGSize(width: 60, height: 60))
//        }
//
//        nameLabel = UILabel.init()
//        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
//        nameLabel.textColor = WHITE_COLOR
//        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
//        containerView.addSubview(nameLabel)
//        nameLabel.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(avatar.snp.bottom).offset(5)
//        }
//
//        identifyLabel = UILabel.init()
//        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
//        identifyLabel.textColor = WHITE_COLOR
//        identifyLabel.font = UIFont.systemFont(ofSize: 12)
//        containerView.addSubview(identifyLabel)
//        identifyLabel.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(nameLabel.snp.bottom).offset(11)
//        }
        
        let itemContainer = UIView.init()
        itemContainer.backgroundColor = UIColor.white
        itemContainer.layer.cornerRadius = 10
        itemContainer.layer.masksToBounds = true
        self.addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-15)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*15, height: 70))
        }
        
        profitItem = createItem(title: "盈利钱包")
        profitItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        itemContainer.addSubview(profitItem)

        gainItem = createItem(title: "收益钱包")
        gainItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        itemContainer.addSubview(gainItem)

        storeItem = createItem(title: "中心钱包")
        storeItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        itemContainer.addSubview(storeItem)

        let itemArray : [TSWithDrawItem] = [profitItem, gainItem, storeItem]
        itemArray.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 5, leadSpacing: 0, tailSpacing: 0)
        itemArray.snp.makeConstraints { (make) in
            make.top.height.equalToSuperview()
        }

        let line1 = UIView.init()
        line1.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(profitItem.snp.right)
            make.size.equalTo(CGSize(width: 1, height: 25))
        }

        let line2 = UIView.init()
        line2.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(storeItem.snp.left)
            make.size.equalTo(CGSize(width: 1, height: 25))
        }
    }
    
    func createItem(title:String) -> TSWithDrawItem {
        
        let item = TSWithDrawItem.init(title)
        return item
    }

}
